/*
1. Two Sum  
Total Accepted: 223716 Total Submissions: 968821 Difficulty: Easy

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.

Subscribe to see which companies asked this question
*/

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        
    }
};

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target) {
    int *ret = malloc(2 * sizeof(int));
    int i, j;

    for (i = 0; i < numsSize; i++){
        // if (target - nums[i]) in sec
        int tt = target - nums[i];
        for (j = i + 1; j < numsSize; j++){
            if (nums[j] == tt){
                ret[0] = i;
                ret[1] = j;
                return ret;
            }
        }
    }
// 保证有结果的话,这里其实走不到
    return ret;
}

// 本来打算拿上面的代码提交的,但是运行以下测试用例的时候
/*
[3,2,4,3,3]
6
*/
// 上面的代码返回[0,3]
// 而标准输出返回[1,2]
// 所以最后用的是下面的代码
vector<int> twoSum(vector<int> &numbers, int target) {
    unordered_map<int, int> src;
    vector<int> ret;
    for(int i=0; i<numbers.size(); i++){
        if (src.find(numbers[i])==src.end()){
            src[target - numbers[i]] = i;
        }else { 
            ret.push_back(src[numbers[i]]);
            ret.push_back(i);
            break;
        }
    }
    return ret;
}

